package com.fiveGroup.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fiveGroup.pojo.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface MessageMapper extends BaseMapper<Message> {

    /**
     * 标记某个会话的消息为已读
     */
    @Update("UPDATE message SET read_status = 1 " +
            "WHERE receiver_id = #{receiverId} AND sender_id = #{senderId} AND read_status = 0")
    void markAsRead(@Param("senderId") Integer senderId,
                    @Param("receiverId") Integer receiverId);

    /**
     * 查询接收者的未读消息总数
     */
    Integer countUnreadMessages(@Param("receiverId") Integer receiverId);

    /**
     * 查询当前用户的双向关注好友（互相关注）
     */
    @Select("""
            SELECT ufs.seller_id AS friendId
            FROM user_follow_seller ufs
            WHERE ufs.user_id = #{userId}
              AND EXISTS (
                SELECT 1
                FROM user_follow_seller ufs2
                WHERE ufs2.user_id = ufs.seller_id
                  AND ufs2.seller_id = #{userId}
              )
            """)
    List<Integer> getMutualFriends(@Param("userId") Integer userId);

    /**
     * 获取聊天记录（双向查询）
     */
    List<Message> selectChatHistory(@Param("userId1") Integer userId1,
                                    @Param("userId2") Integer userId2);

}
